package com.hzy.fivepalace.wx.controller.api;

import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.wx.annotation.LoginUser;
import com.hzy.fivepalace.wx.controller.base.BaseController;
import com.hzy.fivepalace.wx.dao.WxLoginInfo;
import com.hzy.fivepalace.wx.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/wx/auth")
@Validated
@Api(tags = "鉴权服务")
public class WxAuthController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(WxAuthController.class);

    @Resource
    private LoginService loginService;

    /**
     * 获取微信用户的openid
     *
     * @param code
     * @param request
     * @return
     */
    @GetMapping("/getOpenId")
    @ApiOperation(value = "获取微信用户的openid")
    public Object loginByWeixin(@ApiParam(value = "code") @NotBlank String code, HttpServletRequest request) {
        return loginService.getOpenId(code);
    }

    /**
     * 微信登录
     * @param wxLoginInfo
     * @param request
     * @return
     */
    @PostMapping("/weixin_login")
    @ApiOperation(value = "微信登录")
    public Object loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {

        return loginService.toWxLogin(wxLoginInfo, request);
    }

    /**
     * 判断是否登录
     * @param userId
     * @return
     */
    @PostMapping("/isLogin")
    @ApiOperation(value = "判断是否登录")
    public Object isLogin(@LoginUser(login=false) @ApiParam(hidden = true) Integer userId) {

        return ResponseUtil.ok("查询成功",(null!=userId&&userId.intValue()>0));
    }

    /**
     * 退出登录
     *
     * @param userId
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    public Object logout(@ApiParam(hidden = true) @LoginUser(login = false) Integer userId) {
        return loginService.toLogout(userId);
    }


}
